//
//  MessageDAO.m
//  express
//
//  Created by dd on 12/15/14.
//  Copyright (c) 2014 yunqi. All rights reserved.
//

#import "MessageDAO.h"
#import "DXKit.h"

@implementation MessageDAO

+ (void)prepareDB
{
    FMDatabase *db = [self initDB];
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return ;
    }
    
    [db executeUpdate:@" \
     CREATE TABLE IF NOT EXISTS message ( \
     id INTEGER PRIMARY KEY, \
     message_id INTEGER , \
     from_user_id INTEGER , \
     from_user_type INTEGER , \
     to_user_id INTEGER, \
     to_user_type INTEGER, \
     to_group_id INTEGER, \
     created INTEGER, \
     added INTEGER, \
     type INTEGER, \
     content_type INTEGER, \
     content TEXT, \
     status INTEGER, \
     read INTEGER, \
     send INTEGER,\
     name TEXT,\
     avatar TEXT\
     )"
     ];
    
    [db close];
}

+ (NSNumber *)showCont
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return nil;
    }
    
    int64_t count = 0;
    FMResultSet *rs = [db executeQuery:@" select rowid as count from message order by rowid desc limit 1"];
    if ([rs next])
    {
        count = [rs longLongIntForColumn:@"count"];
    }
    NSNumber *con = [NSNumber numberWithLongLong:count];
    return con;
}

+ (NSNumber *)add:(NSDictionary *)msg
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return nil;
    }
    
    int64_t count = 0;
    FMResultSet *rs = [db executeQuery:@" select rowid as count from message order by rowid desc limit 1"];
    if ([rs next])
    {
        count = [rs longLongIntForColumn:@"count"];
    }
    count += 1;
    [DX i:@"数据库的msgid：%@ --条数%d",msg[MESSAGE_KEY_MESSAGE_ID],count];
    NSNumber *mid = [NSNumber numberWithLongLong:count];
    NSNumber *msgId = msg[MESSAGE_KEY_MESSAGE_ID];
    NSNumber *from_user_id = msg[MESSAGE_KEY_FROM_USER_ID];
    NSNumber *from_user_type = msg[MESSAGE_KEY_FROM_USER_TYPE];
    NSNumber *to_user_id = msg[MESSAGE_KEY_TO_USER_ID];
    NSNumber *to_user_type = msg[MESSAGE_KEY_TO_USER_TYPE];
    NSNumber *to_group_id = msg[MESSAGE_KEY_TO_GROUP_ID];
    NSNumber *created = msg[MESSAGE_KEY_CREATED];
    NSNumber *added = [NSNumber numberWithLong:(long)[[NSDate date] timeIntervalSince1970]];
    NSNumber *type = msg[MESSAGE_KEY_TYPE];
    NSString *content = msg[MESSAGE_KEY_CONTENT];
    NSNumber *content_type = msg[MESSAGE_KEY_CONTENT_TYPE];
    NSNumber *status = msg[MESSAGE_KEY_STATUS];
    NSNumber *read = msg[MESSAGE_KEY_READ] ? msg[MESSAGE_KEY_READ] : @0;
    NSNumber *send = msg[MESSAGE_KEY_SEND] ? msg[MESSAGE_KEY_SEND] : @0;
    NSDictionary *courier = [msg objectForKey:@"courier"];
    NSString *name = [courier objectForKey:@"name"];
    NSString *avatar = [courier objectForKey:@"avatar"];

    [db executeUpdate:@" insert into message \
     (id ,message_id, from_user_id, from_user_type, to_user_id, to_user_type, to_group_id, created, added, \
     type, content_type, content, status, read, send, name, avatar) values \
     ( (?), (?), (?), (?), (?), (?), (?), (?), (?),\
     (?), (?), (?), (?), (?), (?), (?), (?) ) ",
     mid,msgId, from_user_id, from_user_type, to_user_id, to_user_type, to_group_id, created, added,
     type, content_type, content, status, read, send,name,avatar];
    
    mid = [NSNumber numberWithLongLong:db.lastInsertRowId];
    [DX i:@"%@",mid];
    
    [db close];
    
    return mid;
}

+ (bool)exists:(NSNumber *)msgId
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return false;
    }
    
    bool exists = NO;
    FMResultSet *rs = [db executeQuery:@" select * from message where message_id = (?) ", msgId];
    if ([rs next])
    {
        exists = YES;
    }
    
    [db close];
    
    return exists;
}

+ (NSDictionary *)getWithId:(NSNumber *)mid
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return nil;
    }
    
    NSDictionary *message = nil;

    FMResultSet *rs = [db executeQuery:@" select * from message where id = (?) ", mid];
    if ([rs next])
    {
        message = [self processMessage:rs];
    }
    
    [db close];
    [DX i:@"message:---%@",message];
    return message;
}

+ (void)dump
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return ;
    }
    
    FMResultSet *rs = [db executeQuery:@" select * from message order by added desc "];
    while ([rs next])
    {
        NSDictionary *message = [MessageDAO processMessage:rs];
        [DX i:@"\n%@\n", message];
    }
    
    [db close];
}

+ (NSDictionary *)processMessage:(FMResultSet *)rs
{
    NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
    message[MESSAGE_KEY_ID] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_ID]];
    message[MESSAGE_KEY_MESSAGE_ID] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_MESSAGE_ID]];
    message[MESSAGE_KEY_FROM_USER_ID] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_FROM_USER_ID]];
    message[MESSAGE_KEY_FROM_USER_TYPE] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_FROM_USER_TYPE]];
    message[MESSAGE_KEY_TO_USER_ID] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_TO_USER_ID]];
    message[MESSAGE_KEY_TO_USER_TYPE] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_TO_USER_TYPE]];
    message[MESSAGE_KEY_TO_GROUP_ID] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_TO_GROUP_ID]];
    message[MESSAGE_KEY_CREATED] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_CREATED]];
    message[MESSAGE_KEY_ADDED] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_ADDED]];
    message[MESSAGE_KEY_TYPE] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_TYPE]];
    message[MESSAGE_KEY_CONTENT_TYPE] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_CONTENT_TYPE]];
    message[MESSAGE_KEY_CONTENT] = [rs stringForColumn:MESSAGE_KEY_CONTENT];
    message[MESSAGE_KEY_STATUS] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_STATUS]];
    message[MESSAGE_KEY_READ] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_READ]];
    message[MESSAGE_KEY_SEND] = [NSNumber numberWithInt:[rs intForColumn:MESSAGE_KEY_SEND]];

    return message;
}

+ (void)truncate
{
    FMDatabase *db = [self initDB];
    
    if (db == nil)
    {
        [DX i:@"init db fail"];
        return ;
    }
    
    [db executeUpdate:@" drop table message "];
    
    [db close];
}


@end
